package com.yunzhou.config;

import com.yunzhou.interceptor.JwtInterceptor;
import com.yunzhou.interceptor.RefreshTokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration  // 配置类也要注解注入到IOC容器中
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Resource // 校验Token的拦截器
    private JwtInterceptor jwtInterceptor;
    @Resource // 刷新Token的拦截器
    private RefreshTokenInterceptor refreshTokenInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .exposedHeaders("Content-Disposition");
    }

    @Override // 添加拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        // 1。刷新Token的拦截器
        registry.addInterceptor(refreshTokenInterceptor)
                .addPathPatterns("/user/**")
                .order(0);

        // 2.校验Token的拦截器
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns(
                        "/user/user/login",
                        "/user/user/register",
                        "/user/user/resetPassword",
                        "/user/user/getUserDetail",
                        "/user/user/sendRegisterEmailCheckCode",
                        "/user/user/sendEmailCheckCodeForResetPassword",
                        "/user/user/getSendEmailImageCheckCode",
                        "/user/user/getLoginImageCheckCode",
                        "/user/user/getRegisterImageCheckCode",
                        "/user/user/getRegisterSendEmailImageCheckCode",
                        "/user/user/sendEmailCheckCode",
                        "/user/user/getUserInfo",
                        "/user/user/getUserDetail",
                        "/user/user/getUserArticle",
                        "/user/user/getUserComment",
                        "/user/forumBoard/getForumBoard",
                        "/user/forumBoard/getForumBoard2",
                        "/user/forumArticle/getForumArticleList",
                        "/user/forumArticle/getArticleCover/{articleId}",
                        "/user/forumArticle/getForumArticleDetail",
                        "/user/forumArticle/getForumArticleListByUserId",
                        "/user/forumArticle/searchForumArticle",
                        "/user/forumArticle/getRelativeForumArticle",
                        "/user/forumArticle/getHotArticle",
                        "/user/forumComment/getForumCommentList",
                        "/user/forumComment/getMyForumCommentList",
                        "/user/forumComment/getMyPostForumCommentList",
                        "/user/likeRecord/getLikeRecordByUserIdAndOpType",
                        "/user/ai/getChatHistory",
                        "/user/ai/chat4Stream"
                        )
                .order(1);
    }
}
